package com.example.tse.rbyair.PKhome.adapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;

import com.example.tse.rbhttpservice.service.goods.model.GoodsGetVirtualCatListList;
import com.example.tse.rbyair.PKhome.fragment.MainSortItemFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hp on 16/1/25.
 */
public class MainFragmentStatePagerAdapter extends FragmentStatePagerAdapter {

    private List<GoodsGetVirtualCatListList> lists = new ArrayList<GoodsGetVirtualCatListList>();
    private ArrayList<ArrayList<GoodsGetVirtualCatListList>> lists2 ;

    public MainFragmentStatePagerAdapter(FragmentManager fm ,List<GoodsGetVirtualCatListList> lists) {
        super(fm);
        this.lists.clear();
        this.lists.addAll(lists);

        lists2 = createList(lists, 4);
//        L.d("hp", "MainFragmentStatePagerAdapter MainFragmentStatePagerAdapter:" + lists2.size() + " " + lists2.get(0).size());

    }

    private  ArrayList<ArrayList<GoodsGetVirtualCatListList>>  createList(List<GoodsGetVirtualCatListList> targe,int size) {
        ArrayList<ArrayList<GoodsGetVirtualCatListList>> listArr = new ArrayList<ArrayList<GoodsGetVirtualCatListList>>();
        int arrSize = targe.size()%size==0?targe.size()/size:targe.size()/size+1;
        for(int i=0;i<arrSize;i++) {
            ArrayList<GoodsGetVirtualCatListList>  sub = new ArrayList<GoodsGetVirtualCatListList>();
            //把指定索引数据放入到list中
            for(int j=i*size;j<=size*(i+1)-1;j++) {
                if(j<=targe.size()-1) {
                    sub.add(targe.get(j));
                }
            }
            listArr.add(sub);
        }
        return listArr;
    }

//	public void setData(List<GoodsGetCategoryListNew> lists){
//		this.lists.clear();
//		this.lists.addAll(lists);
//		this.lists2.clear();
//
//		RbLog.i("MainFragmentStatePagerAdapter ="+lists.size());
//
//		for(int i=0;i<lists.size();i++){
//			List<GoodsGetCategoryListNew> l = new ArrayList<GoodsGetCategoryListNew>();
//			l.add(lists.get(i));
//			RbLog.i("l.add(lists.get(i));"+i);
//			if((i+1)%4==0 || i==lists.size()-1){
//				lists2.add(l);
//				RbLog.i("lists2.add(l);"+i);
//			}
//		}
//
//		notifyDataSetChanged();
//	}

    @Override
    public Fragment getItem(final int position) {
        MainSortItemFragment f = new MainSortItemFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("lists", lists2.get(position));
        f.setArguments(bundle);
        return f ;
    }


    @Override
    public Object instantiateItem(ViewGroup arg0, int arg1) {
        return super.instantiateItem(arg0, arg1);
    }

    @Override
    public int getCount() {

        return lists2.size();
    }
}
